package com.github.liuche51.easyTaskL.controller;

import com.github.liuche51.easyTask.core.AnnularQueue;
import com.github.liuche51.easyTask.core.TaskType;
import com.github.liuche51.easyTask.core.TimeUnit;
import com.github.liuche51.easyTask.monitor.AnnularQueueMonitor;
import com.github.liuche51.easyTask.monitor.ClusterMonitor;
import com.github.liuche51.easyTaskL.service.TestService;
import com.github.liuche51.easyTaskL.task.CusTask1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.ZonedDateTime;
import java.util.*;

@Controller
@RequestMapping("/s")
public class SingleController {
    private static Logger log = LoggerFactory.getLogger(SingleController.class);
    @Value("${easyTaskL.serverPort}")
    private int serverPort;
    @Autowired
    private TestService testService;

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("single.jsp");
        view.addObject("node", serverPort);
        return view;
    }

    @RequestMapping("/once")
    @ResponseBody
    public String once(@RequestParam("name") String name, @RequestParam("time") int time) {
        CusTask1 task1 = new CusTask1();
        task1.setEndTimestamp(ZonedDateTime.now().plusSeconds(time).toInstant().toEpochMilli());
        Map<String, String> param = new HashMap<String, String>() {
            {
                put("name", name);
                put("birthday", "1996-1-1");
                put("age", "28");
                put("threadid", String.valueOf(Thread.currentThread().getId()));
            }
        };
        task1.setParam(param);
        try {
            return AnnularQueue.getInstance().submitAllowWait(task1);
        } catch (Exception e) {
            log.error("", e);
            return e.getMessage();
        }

    }

    @RequestMapping("/period")
    @ResponseBody
    public String period(@RequestParam("name") String name, @RequestParam("period") int period, @RequestParam("immediately") int immediately) {
        CusTask1 task2 = new CusTask1();

        task2.setEndTimestamp(ZonedDateTime.now().plusSeconds(immediately == 1 ? -1 : 1).toInstant().toEpochMilli());
        task2.setTaskType(TaskType.PERIOD);
        task2.setUnit(TimeUnit.SECONDS);
        Map<String, String> param2 = new HashMap<String, String>() {
            {
                put("name", name);
                put("birthday", "1996-1-1");
                put("age", "28");
                put("threadid", String.valueOf(Thread.currentThread().getId()));
            }
        };
        task2.setParam(param2);
        try {
            task2.setPeriod(period);
            return AnnularQueue.getInstance().submitAllowWait(task2);
        } catch (Exception e) {
            log.error("", e);
            return e.getMessage();
        }
    }

    @RequestMapping("/taskInfo")
    @ResponseBody
    public List<String> taskInfo() {
        List<String> list = new LinkedList<>();
        while (CusTask1.queue.size() > 0) {
            list.add(CusTask1.queue.poll());
        }
        return list;
    }

    @RequestMapping("/clearTaskInfo")
    @ResponseBody
    public String clearTaskInfo() {
        CusTask1.queue.clear();
        return "true";
    }

    @RequestMapping("/searchDbInfo")
    @ResponseBody
    public Map<String, Map<String, List>> searchDbInfo(@RequestParam("taskId") String taskId) {
        try {
            Map<String, Map<String, List>> map = ClusterMonitor.getDBTraceInfoByTaskId(taskId);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/getPoolsInfo")
    @ResponseBody
    public Map<String, Object> getPoolsInfo() {
        Map<String, Object> map = new HashMap<>(3);
        try {
            map.put("waiteExcuteQty", AnnularQueueMonitor.getTaskInAnnularQueueQty());
            map.put("dispatch", AnnularQueueMonitor.getDispatchsPoolInfo());
            map.put("worker", AnnularQueueMonitor.getWorkersPoolInfo());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
